<?php
namespace Modules\Market\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Market\Entities\ExchangeRecord;

use Validator;
use ThrowException;
use Auth;
use DB;

class ExchangeRecordController extends Controller
{
    // 各种状态的数量统计
    public function statusTotal(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $options = $req->all();

        $total = [
            'pending' => ExchangeRecord::getBuilder($options)->status('pending')->count(),
            'completed' => ExchangeRecord::getBuilder($options)->status('completed')->count(),
            'all' => ExchangeRecord::getBuilder($options)->count(),
        ];

        return [
            'total' => $total
        ];
    }


    // 获取开奖列表
    public function index(Request $req)
    {
        $rule = [
            // 'activity_type' => ['required'],
            // 'activity_id' => ['required'],
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        // $class = @config('activity.map')[$activityType] ?: ThrowException::Conflict('无效的活动类型');

        $where = [];

        $options = $req->all();
        $options = array_merge($options, [
            'where' => $where,
            'with' => ['user', 'package_skus', 'product_sku'],
            'only' => [
                'sid',
                'id',
                'uuid',
                'created_at',
                'source_money_price',
                'source_score_price',
                'package_skus',
                'order_number',
                'union_status',
                'union_status_text',
                'complete_type',
                'product_total',
                'is_allow_fast_match',
                'user' => [
                    'headimg',
                    'name',
                    'phone'
                ],
                'product_sku' => [
                    'title',
                    'thumb',
                    'money_price',
                    'score_price'
                ]
            ]
        ]);

        $list = ExchangeRecord::getList($options);

        return $list->generateListResponse();
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = ExchangeRecord::getEntity([
            'uuid' => $uuid
        ]);

        $item->close(true);

        $item->delete();

        return [];
    }
}
